/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  Console
 * 
 * Description of class:
 * 
 * This is the Console of the UI. It displays error messages and standard
 * output.
 * 
 */

package karel.UI;

import java.awt.Color;
import java.awt.Font;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import karel.util.FontLoader;

public class Console extends javax.swing.JPanel {

    private static Document doc; 
    private static SimpleAttributeSet standardText;
    private static SimpleAttributeSet errorText;
    
    /**
     * Simply a constructor
     */
    public Console() {
        
        initComponents();
        
        doc = consoleTextPane.getDocument();
        
        standardText = new SimpleAttributeSet();
        StyleConstants.setForeground(standardText, Color.BLACK);
        StyleConstants.setBackground(standardText, Color.WHITE);
        
        
        errorText = new SimpleAttributeSet();
        StyleConstants.setForeground(errorText, Color.RED);
        StyleConstants.setBackground(errorText, Color.WHITE);
        StyleConstants.setBold(errorText,true);
    }
    
    /**
     * Clears the console of any text.
     */
    public static void clearConsole() {
        try {
            doc.remove(0, doc.getLength());
        } catch (BadLocationException ex) {
            Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Prints input string to the console.
     * @param string 
     */
    public static void print(String string) {
        try {
            doc.insertString(doc.getLength(), string, standardText);
        } catch (BadLocationException ex) {
            Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Prints input string to the console followed by a new line.
     * @param string 
     */
    public static void println(String string) {
        print(string + "\n");
    }
    
    /**
     * Prints input string to the console in error colors followed by a new line.
     * @param string 
     */
    public static void error(String string) {
        try {
            doc.insertString(doc.getLength(), string + "\n", errorText);
        } catch (BadLocationException ex) {
            Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * Here below is auto generated code from NetBeans IDE 7.2.1
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        consoleTextPane = new javax.swing.JTextPane();

        setInheritsPopupMenu(true);
        setLayout(new java.awt.BorderLayout());

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        consoleTextPane.setEditable(false);
        consoleTextPane.setFont(karel.util.FontLoader.getDefaultFont(Font.PLAIN,FontLoader.ConsoleFontSize));
        scrollPane.setViewportView(consoleTextPane);

        add(scrollPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane consoleTextPane;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}
